/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.utils;

import com.reddys.rad.inventory.db.Stock;
import com.reddys.rad.inventory.db.StockDetails;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.persistence.EntityManager;

/**
 *
 * @author REDDYS
 */
public class Utilities {
    static FileHandler hand = null;
    
    static Popup popup = null;
    public static Date getCurrentDate(){
        
        return new Date();
    }
    
    public static String getDisplayDate(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String formattedDate = sdf.format(date);
        return formattedDate;
    }
    
    public static void main(String args[]){
        Utilities.createDialog("");
    }
    
    public static void createDialog(String text){
       final Stage dialogStage = new Stage();
        Button btn = new Button("Ok");
        btn.setOnMouseClicked(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent t) {
               dialogStage.close();
            }
            
        });
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.setScene(new Scene(VBoxBuilder.create().
        children(new Text(text), btn).
        alignment(Pos.CENTER).build(),400,120));
        dialogStage.show();
    }

    public static boolean isNull(String text){
        boolean boo  = true;
        if(text !=null && text!="" && !text.equalsIgnoreCase("")){
            boo = false;
        }
        return boo;
    }
    
    public static Logger getLogger(String obj){
        Logger logger = null;
             logger = Logger.getLogger(obj);
             logger.addHandler(getFileHandler());
        return logger;
    }
    
    public static FileHandler getFileHandler(){
        if(null == hand){
            try {
                hand = new FileHandler("Inventory.log");
            } catch (IOException ex) {
                Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return hand;
    }
    
        public static void updateStock(Stock stock,EntityManager em) {
       if(Utilities.isNull(stock.getStockId())){
            em.persist(stock);
       }else{
            em.merge(stock);
       }
       
        }

    public static void updateStockDetails(StockDetails stockDetails, EntityManager em) {
       if(Utilities.isNull(stockDetails.getStockDetailId())){
        em.persist(stockDetails);
       }else{
            em.merge(stockDetails);
       }
    }
    
   public static Popup createPopup(Node node,Node window)
    {
        popup = new Popup();
        popup.getContent().add(node);
        popup.centerOnScreen();
        popup.setAutoHide(true);
        popup.show(window,120,120);
        return popup;
    }
        
    public static void hidePopup(Popup popup)
    {
        popup.hide();
    }
    
    public static void createCellValueFactory(TableColumn tableColumn,int columnNo){
        final int columnIndex = columnNo;     
        tableColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
             
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( 
                      cdf.getValue().get(columnIndex)!=null?cdf.getValue().get(columnIndex).toString():"");
            }
              });

    }
}
